package cn.azr.domain.strategy.service.rule.filter.impl;

import cn.azr.domain.strategy.model.entity.RuleActionEntity;
import cn.azr.domain.strategy.model.entity.RuleMatterEntity;
import cn.azr.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import cn.azr.domain.strategy.repository.IStrategyRepository;
import cn.azr.domain.strategy.service.annotation.LogicStrategy;
import cn.azr.domain.strategy.service.rule.filter.ILogicFilter;
import cn.azr.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * ClassName:RuleLockLogicFilter
 * Description:
 * 用户抽奖n次后，解锁奖品
 * @version 1.0
 * @Author azr
 * @Create 2024/10/13 10:59
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_LOCK)
public class RuleLockLogicFilter implements ILogicFilter<RuleActionEntity.RaffleCenterEntity> {


    //仓储
    @Resource
    private IStrategyRepository strategyRepository;

    private Long userRaffleCount=0L;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleCenterEntity> filter(RuleMatterEntity ruleMatterEntity) {
        log.info("规则过滤-次数锁 userId:{} strategyId:{} ruleModel:{}", ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());
        //查看对应奖品rule_lock解锁值

        String ruleValue=strategyRepository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(),ruleMatterEntity.getAwardId(),ruleMatterEntity.getRuleModel());
        Long ruleRaffleCount=Long.parseLong(ruleValue);

        //如果已抽取次数大于rule_count，则放行，返回抽奖产品
        if(userRaffleCount>=ruleRaffleCount){
            return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                    .build();
        }
        return RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
                .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                .build();

    }
}
